export default {
  data() {
    return {
      placeholder: '请输入价格',
      inColour: [],
      show: false,
      downShow: false, //下拉显示
      isChoose: 0, //是否选择内饰
      isChooseW: 0, //是否选择外观
      //选择配色列表
      chooseColorList: [{
        id: 0,
        color: 'white',
        color2: 'black',
        title: '极光+内饰  泰坦黑'
      },
      {
        id: 1,
        color: 'black',
        color2: 'white',
        title: '泰坦黑  极光+内饰'
      },
      {
        id: 2,
        color: 'white',
        color2: 'white',
        title: '极光+内饰  泰坦白'
      },
      {
        id: 3,
        color: 'white',
        color2: 'red',
        title: '极光+内饰  泰坦红'
      },
      ],
      //输入内容
      list: {
        color: [], //颜色
        totalMoney: 0, //落地总金额
        carMoney: '', //裸车价
        otherTotalMoney: 0, //必要花费
        purchaseTax: 0, //购置税
        insuranceMoney: 0, //交强险
        vehicleUseTax: 0, //车船使用税
        consumptionTax: 0, //消费税
        serviceFee: '', //综合服务费
        commercialInsurance: '', //商业保险
        carFindOtherFeeList: [{
          'item_Description': '', //项目描述
          'item_Fee': '' //费用
        }],
      },
      color: '',
      swiperList: [], //传过来的swiper
      dd: [],
      taddress: [], //地址id
      addList: [], //地址名称
      params: {
        province: true,
        city: true,
        area: false
      },
      a: {}, //默认外饰
      b: {}, //默认内饰
      // purchase: 0,//税
      reg: 0,
      sum: 0,
      sum2: 0,
    }
  },
  watch: {
    'list.carMoney'(val) {
      if (val === '') {
        return false
      }
      val = (val.match(/^\d*(\.?\d{0,2})/g)[0]) || null
      // console.log(val,'监听')
      // this.getCarFindTax(e)
      this.$u.debounce(this.getCarFindTax, 500)
      this.$nextTick(() => {
        this.list.carMoney = val
      })
    },
    // 'list.serviceFee'(val) {
    //   if (val === '') {
    //     this.list.serviceFee = ''
    //   }
    // },
    'list.commercialInsurance'(val) {
      val = (val.match(/^\d*(\.?\d{0,2})/g)[0]) || null
      console.log(val, '监听')
      this.$nextTick(() => {
        this.list.commercialInsurance = val
      })
    }
  },
  computed: {
    //落地总金额
    c() {
      this.bulr()
      let coSum = this.sum
      let sum = this.list.otherTotalMoney +
				parseFloat(Number(this.list.commercialInsurance)) +
				parseFloat(Number(this.list.carMoney)) + parseFloat(coSum)
      this.list.totalMoney = sum
      return sum
    },
    //必要花费
    d() {
      return this.list.otherTotalMoney = parseFloat(Number(this.list.serviceFee)) +
				this.list.purchaseTax +
				this.list.insuranceMoney +
				this.list.vehicleUseTax +
				this.list.consumptionTax
    }
  },
  onLoad(op) {
    let list = JSON.parse(op.list)
    this.swiperList = list
    console.log(list)
    if(list.type===1){
      this.color = list.colour
      console.log(this.color, 'list')
    }else{
      this.color = JSON.parse(list.colour)
      let coding = {
        color: '色全',
        colorCoding: 'linear-gradient(0deg,rgba(255,49,49,1),rgba(255,243,52,1),rgba(37,191,255,1))'
      }
      this.a = this.color.inColour[0] || coding //内饰
      this.b = this.color.outerColour[0] || coding //外观
    }
    this.list.color = {
      inColour: [this.a],
      outerColour: [this.b]
    }
  },
  filters: {
  	million(value) { //过万处理
  		let num
  		if (value > 9999) { //大于9999显示x.xx万
  			num = ((value / 1000) / 10).toFixed(2) + '万'
  		} else if (value <= 9999 && value >= -9999) {
  			num = value + '元'
  		} else if (value < -9999) { //小于-9999显示-x.xx万
  			num = -((Math.abs(value) / 1000) / 10).toFixed(2) + '万'
  		}
  		return num
  	}
  },
  methods: {
    //计算自定义价格
    bulr() {
      let sum = 0
      this.list.carFindOtherFeeList.forEach(item => {
        sum += parseFloat(Number(item.item_Fee))
      })
      this.sum = sum
    },
    async getCarFindTax() {
      let data = {
        'configuration_Id': this.swiperList.configuration_Id,
        'car_Money': parseFloat(Number(this.list.carMoney))
      }
      try {
        const res = await this.$api.comparison.getCarFindTax(data)
        console.log(res, '1')
        this.list.purchaseTax = res.data.purchaseTax
        this.list.insuranceMoney = res.data.insuranceMoney
        this.list.vehicleUseTax = res.data.vehicleUseTax
        this.list.consumptionTax = res.data.consumptionTax
      } catch (e) {
        //TODO handle the exception
      }
    },
    //裸车价
    blur(e) {
      this.list.carMoney = e
      console.log(e)
      // this.getCarFindTax(e)
    },
    //删除自定义
    del(e) {
      let id = Number(e.currentTarget.id)
      this.list.carFindOtherFeeList.splice(id, 1)
    },
    addInput() {
      this.list.carFindOtherFeeList.push({
        'item_Description': '', //项目描述
        'item_Fee': '' //费用
      })
      // if(this.list.carFindOtherFeeList.length>1){
      // this.bulr()
      // }
    },

    confirm(e) {
      console.log(e)
      this.taddress.city = e.city.id
      this.addList.city = e.city.label //市
      this.addList.province = e.province.label //省
      this.taddress.city = e.city.id //市
      this.taddress.province = e.province.id //省
      this.show = false
    },
    aaa() {
      this.show = true
    },

    //提交
    async addConfim() {
      if (Number(this.list.carMoney) === 0) {
        this.$refs.uToast.show({
          title: '裸车价不能为0',
          type: 'info',
        })
        return false
      }
      if (!this.taddress.city) {
        this.$refs.uToast.show({
          title: '地址不能为空',
          type: 'info',
        })
        return false
      }
			 // || this.list.carFindOtherFeeList[i].item_Fee !== ''
      for (let i in this.list.carFindOtherFeeList) {
        if (this.list.carFindOtherFeeList[i].item_Description !== '') {
          if(this.list.carFindOtherFeeList[i].item_Fee === ''){
            this.$refs.uToast.show({
						  title: '价格或名称不能为空',
						  type: 'info',
            })
            return false
          }
        } else if(this.list.carFindOtherFeeList[i].item_Fee !== ''){
          if(this.list.carFindOtherFeeList[i].item_Description === ''){
            this.$refs.uToast.show({
						  title: '价格或名称不能为空',
						  type: 'info',
            })
            return false
          }
        }else {
          this.list.carFindOtherFeeList = []
        }
      }
      try {
        // parseFloat(c).toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
        let total = parseFloat(this.list.totalMoney).toFixed(2)
        let otherTotalMoney = parseFloat(this.list.otherTotalMoney).toFixed(2)
        console.log(total)
        let data = {
          'brand_Id': Number(this.swiperList.brand_Id), //品牌id
          'brand_Name': this.swiperList.brand_Name, //品牌名称
          'car_Type_Id': Number(this.swiperList.car_Type_Id), //系列id
          'car_Type_Name': this.swiperList.car_Type_Name, //系列名称
          'configuration_Id': Number(this.swiperList.configuration_Id), //配置id
          'configuration_Name': this.swiperList.configuration_Name, //配置名称
          'colour': JSON.stringify(this.list.color), //颜色
          'total_Money': Number(total), //落地总金额
          'car_Money': Number(this.list.carMoney), //裸车价
          'other_Total_Money': Number(otherTotalMoney), //必要花费
          'purchase_Tax': this.list.purchaseTax, //购置税
          'insurance_Money': this.list.insuranceMoney, //交强险
          'vehicle_Use_Tax': this.list.vehicleUseTax, //车船使用税
          'consumption_Tax': this.list.consumptionTax, //消费税
          'service_Fee': Number(this.list.serviceFee), //综合服务费
          'commercial_Insurance': Number(this.list.commercialInsurance), //商业保险
          'city': this.taddress.city, //上户城市
          'province': this.taddress.province, //上户省
          'carFindOtherFeeList': this.list.carFindOtherFeeList
        }
        const res = await this.$api.comparison.addCarFind(data)
        // console.log(res)

        if (res.code === 200) {
          this.$refs.uToast.show({
            title: '提交成功',
            type: 'success',
            back: true
          })
        }
      } catch (e) {

      }
    },

    //新增询比价信息
    addCarFind() {
      this.$u.throttle(this.addConfim, 3000)
    },
    //选择配色外饰
    chooseColor(id, item) {
      console.log(item)
      this.isChoose = id
      this.b = item
      this.list.color = {
        inColour: [this.a],
        outerColour: [this.b]
      }
      // console.log(this.list.color,'内饰')
      // this.list.color = JSON.stringify(item)
    },
    //选择配色内饰
    chooseColors(id, item) {
      console.log(item)
      this.isChooseW = id
      this.a = item
      this.list.color = {
        inColour: [this.a],
        outerColour: [this.b]
      }
    },
    //比要花费下拉
    down() {
      this.downShow = !this.downShow
    },
  }
}
